//
//  DCSystemContactsController.m
//  Demo
//
//  Created by zhaolin on 2023/2/1.
//

#import "DCSystemContactsController.h"
#import <Contacts/Contacts.h>

@interface DCSystemContactsController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *dataSource;
@end

@implementation DCSystemContactsController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitleName:@"System Contacts"];
    [self getContactData];
}
- (void)configureUI {
    [super configureUI];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    [self.tableView registerClass:UITableViewCell.class forCellReuseIdentifier:NSStringFromClass(UITableViewCell.class)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = self.view.backgroundColor ;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.navgitBar.mas_bottom);
        make.left.right.bottom.mas_equalTo(0);
    }];
}

- (void)configureAlloc {
    [super configureAlloc];
    self.dataSource = @[].mutableCopy;
}

- (void)getContactData{
    
    NSArray *keysToFetch = @[CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey];
    CNContactFetchRequest *fetchRequest = [[CNContactFetchRequest alloc] initWithKeysToFetch:keysToFetch];
    fetchRequest.sortOrder = CNContactSortOrderFamilyName;
    CNContactStore *contactStore = [[CNContactStore alloc] init];

    [contactStore enumerateContactsWithFetchRequest:fetchRequest error:nil usingBlock:^(CNContact * _Nonnull contact, BOOL * _Nonnull stop) {

        NSString * firstName = contact.familyName;
        NSString * lastName = contact.givenName;

        //电话
        NSArray * phoneNums = contact.phoneNumbers;
        CNLabeledValue *labelValue = phoneNums.firstObject;
        NSString *phoneValue = [labelValue.value stringValue];

        NSLog(@"姓名：%@%@ 电话：%@", firstName, lastName, phoneValue);
        if(phoneValue && phoneValue.length) {
            [self.dataSource addObject:[NSString stringWithFormat:@"%@%@  %@",firstName,lastName,phoneValue]];
        }
        [self.tableView reloadData];
    }];
    
}

#pragma mark - tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *string = [self.dataSource objectAtIndex:indexPath.row];
    NSArray *contactdata0= [string componentsSeparatedByString:@"  "];
    NSString *name = [contactdata0 firstObject];
    NSString *phoneNum = [contactdata0 lastObject];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(UITableViewCell.class)];
    cell.textLabel.text = [NSString stringWithFormat:@"name:%@  phone:%@",name,phoneNum];
    cell.textLabel.font = DCFont_PF_Regular(15);
    cell.textLabel.textColor = [UIColor colorWithString:@"#1C221B"];
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *string = [self.dataSource objectAtIndex:indexPath.row];
    if(self.delegate && [self.delegate respondsToSelector:@selector(systemContactsSelect:)]) {
        [self.delegate systemContactsSelect:string];
    }
    [self.navigationController popViewControllerAnimated:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
